package com.bw.house.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.bw.house.entity.HouseNewroomEntity;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bw.house.service.HouseNewroomService;
import com.bw.common.utils.PageUtils;
import com.bw.common.utils.R;



/**
 * 新房/二手房表
 *
 * @author Hao_YunKai
 * @email 2745578447@qq.com
 * @date 2022-03-23 11:46:46
 */
@RestController
@RequestMapping("house/house-new-room")
public class HouseNewroomController {
    @Autowired
    private HouseNewroomService houseNewroomService;

    @SuppressWarnings("all")
    @Autowired
    private RestHighLevelClient client;

    /**
     * 列表
     */
    @PostMapping("/list")
    public R list(@RequestParam Map<String, Object> params) throws IOException {
        PageUtils page = houseNewroomService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{roomId}")
    public R info(@PathVariable("roomId") Long roomId){
		HouseNewroomEntity houseNewroom = houseNewroomService.getById(roomId);

        return R.ok().put("houseNewroom", houseNewroom);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody HouseNewroomEntity houseNewroom){
		houseNewroomService.save(houseNewroom);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody HouseNewroomEntity houseNewroom){
		houseNewroomService.updateById(houseNewroom);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] roomIds){
		houseNewroomService.removeByIds(Arrays.asList(roomIds));

        return R.ok();
    }

    @RequestMapping("findone")
    public HouseNewroomEntity findone(@RequestParam("s") String id) {
        HouseNewroomEntity byId = houseNewroomService.getById(id);
        return byId;
    }


    @RequestMapping("/SynchronousEs")
    public R SynchronousEs() throws IOException {

        List<HouseNewroomEntity> list = houseNewroomService.list();
        BulkRequest request = new BulkRequest();
        for (HouseNewroomEntity houseNewroomEntity : list) {
            IndexRequest request1 = new IndexRequest("housenewroom");
            request1.id(houseNewroomEntity.getRoomId()+"");
            String s = JSON.toJSONString(houseNewroomEntity);
            request1.source(s, XContentType.JSON);
            request.add(request1);
        }
        BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT);
        return R.ok().put("result","同步es成功");
    }

}
